﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Entity;

namespace Tunnel.Tools.FcmsTools
{
    public partial class FcmsPreviewForm : Form
    {
        
        //private int deltX = 5;
        //private int deltY = 5;
        private DisplayElement elment = new DisplayElement();
        
        private int h;
        
        //private int n;
        //private int newDir = -1;
        
        private int realHeight = 96; //屏幕真实大小
        private int realWidth = 128; //屏幕真实大小
        private int realX;
        
        private int realY;
        
        private string s_height;
        private string s_width;
        private int scrCoeff = 7;
        
        private string simFont = "宋体";
        private int simFontSize = 9;
        
        /// <summary>
        /// 预览窗体
        /// </summary>
        /// <param name="_elment">单一屏幕对象</param>
        public FcmsPreviewForm(DisplayElement _elment)
        {
            InitializeComponent();
            this.elment = _elment;
            this.s_height = realHeight.ToString();
            this.s_width = realWidth.ToString();
        }

        private void colorcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.colorcomboBox.SelectedIndex)
            {
                case 0:
                    this.lableTextPrewiew.ForeColor = Color.Red;
                    return;

                case 1:
                    this.lableTextPrewiew.ForeColor = Color.LawnGreen;
                    return;

                case 2:
                    this.lableTextPrewiew.ForeColor = Color.DeepSkyBlue;
                    return;

                case 3:
                    this.lableTextPrewiew.ForeColor = Color.Yellow;
                    return;
            }
        }

        private void fontSizecomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.fontSizecomboBox.SelectedIndex)
            {
                case 0:
                    this.simFontSize = 0x10 * this.scrCoeff;
                    break;

                case 1:
                    this.simFontSize = 0x18 * this.scrCoeff;
                    break;

                case 2:
                    this.simFontSize = 0x20 * this.scrCoeff;
                    break;

                case 3:
                    this.simFontSize = 0x30 * this.scrCoeff;
                    break;
                case 4:
                    this.simFontSize = 0x40 * this.scrCoeff;
                    break;
            }
            this.lableTextPrewiew.Font = new Font(this.simFont, (float)this.simFontSize, GraphicsUnit.Pixel);
            this.textMiddle();
            //if (this.leftradioButton.Checked)
            //{
            //    this.textLeft();
            //}
            //else if (this.middleradioButton.Checked)
            //{
            //    this.textMiddle();
            //}
            //else if (this.resetradioButton.Checked)
            //{
            //    this.textReset();
            //}
        }

        public static int GetLength(string str)
        {
            if (str.Length == 0)
            {
                return 0;
            }
            ASCIIEncoding encoding = new ASCIIEncoding();
            int num = 0;
            byte[] bytes = encoding.GetBytes(str);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] == 0x3f)
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
            }
            return num;
        }

        private void leftradioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.textLeft();
        }

        private void middleradioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.textMiddle();
        }
        private int MaxLineLength()
        {
            int length = this.msgtextBox.Lines.Length;
            int num2 = 0;
            for (int i = 0; i < length; i++)
            {
                if (GetLength(this.msgtextBox.Lines[i].ToString()) >= num2)
                {
                    num2 = GetLength(this.msgtextBox.Lines[i].ToString());
                }
            }
            return num2;
        }
        public bool IsDBC(char c)
        {
            if(c>127)
            {
                return true;
            }
            return false;
        }
        public int CountDBC(string text)
        {
            int count = 0;
            foreach (char c in text)
            {
                if(IsDBC(c))
                {
                    count++;
                }
            }
            return count;
        }
        public int CountNotDBC(string text)
        {
            int count = 0;
            foreach (char c in text)
            {
                if (!IsDBC(c))
                {
                    count++;
                }
            }
            return count;
        }

        private void InsertEnterForTextbox()
        {
            string enter = "\r\n";

            
            string str1 = "";
            string str2 = "";
            
            string result = "";
            string result2 = "";
            string[] lines = msgtextBox.Lines;
            if (msgtextBox.Lines.Length>0)
            {
                int count3 = 0;
                int index3 = 0;
                foreach (char z in msgtextBox.Lines[0])
                {

                    if (!IsDBC(z))
                    {
                        count3 = count3 + 1;
                        index3 = index3 + 1;
                    }
                    if (IsDBC(z))
                    {
                        count3 = count3 + 2;
                        index3 = index3 + 1;
                    }

                }
                if (count3 > 10)
                {
                    string msg = msgtextBox.Text.Replace(enter, "");
                    int count = 0;
                    int index = 0;
                    foreach (char x in msg)
                    {

                        if (!IsDBC(x))
                        {
                            count = count + 1;
                            index = index + 1;
                        }
                        if (IsDBC(x))
                        {
                            count = count + 2;
                            index = index + 1;
                        }
                        if (count < 10)
                        {
                            result = msg;
                        }

                        if (count == 11 || count == 12)
                        {
                            msg = msg.Insert(index, enter);
                            result = msg;
                            str1 = msg.Substring(0, index + 2);
                            str2 = msg.Substring(index + 2);                            
                        }
                    }
                    if (str2 != "")
                    {
                        int count2 = 0;
                        int index2 = 0;
                        foreach (char y in str2)
                        {
                            if (!IsDBC(y))
                            {
                                count2 = count2 + 1;
                                index2 = index2 + 1;
                            }
                            if (IsDBC(y))
                            {
                                count2 = count2 + 2;
                                index2 = index2 + 1;
                            }
                            if (count < 10)
                            {
                                result2 = str2;
                            }

                            if (count2 == 11 || count2 == 12)
                            {
                                str2 = str2.Insert(index2, enter);
                                result = str1 + str2;
                                

                                //str3 = str3.Substring(index + 2);
                            }
                        }
                    }
                    msgtextBox.TextChanged -= msgtextBox_TextChanged;
                    int i = msgtextBox.SelectionStart;
                    msgtextBox.Text = result;
                    this.msgtextBox.Select(i, 0);
                    msgtextBox.TextChanged += msgtextBox_TextChanged;
                }
                
            }


        }

        private void msgtextBox_TextChanged(object sender, EventArgs e)
        {
            this.h = this.MaxLineLength();
            //InsertEnterForTextbox();
            this.lableTextPrewiew.Text = this.msgtextBox.Text;
            //以下代码为防止行数超过屏幕最大限制
            if ((this.simFontSize == (0x10 * this.scrCoeff)) && (this.msgtextBox.Lines.Length > (this.realHeight / 0x10)))
            {
                this.lableTextPrewiew.Text = this.msgtextBox.Lines[0] + "\n" + this.msgtextBox.Lines[1] + "\n" + this.msgtextBox.Lines[2];
            }
            else if ((this.simFontSize == (0x18 * this.scrCoeff)) && (this.msgtextBox.Lines.Length > (this.realHeight / 0x18)))
            {
                this.lableTextPrewiew.Text = this.msgtextBox.Lines[0] + "\n" + this.msgtextBox.Lines[1];
            }
            else if ((this.simFontSize == (0x20 * this.scrCoeff)) && (this.msgtextBox.Lines.Length > (this.realHeight / 0x20)))
            {
                this.lableTextPrewiew.Text = this.msgtextBox.Lines[0];
            }
            else if ((this.simFontSize == (0x30 * this.scrCoeff)) && (this.msgtextBox.Lines.Length > (this.realHeight / 0x30)))
            {
                this.lableTextPrewiew.Text = this.msgtextBox.Lines[0];
            }
            //this.msgtextBox.Select(this.msgtextBox.TextLength, 0);
            textMiddle();
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (this.h > ((this.realWidth / (this.simFontSize / this.scrCoeff)) * 2))
            {
                MessageBox.Show(((this.simFontSize / this.scrCoeff)).ToString() + "号字体每行最多显示" + ((this.realWidth / (this.simFontSize / this.scrCoeff))).ToString() + "个字");
            }
            else
            {
                int length = this.msgtextBox.Lines.Length;
                switch (length)
                {
                    case 0:
                        this.elment.Msg = this.msgtextBox.Text;
                        break;

                    case 1:
                        this.elment.Msg = this.msgtextBox.Text;
                        break;

                    default:
                        {
                            int index = 0;
                            string str2 = "";
                            while (index < length)
                            {
                                str2 = str2 + this.msgtextBox.Lines[index] + @"\n";
                                index++;
                            }
                            this.elment.Msg = str2.Remove(str2.Length - 2, 2);
                            break;
                        }
                }
                this.elment.MsgX = this.realX.ToString("000");
                this.elment.MsgY = this.realY.ToString("000");
                this.elment.Font = this.fontcomboBox.SelectedItem.ToString();
                this.elment.FontSize = this.fontSizecomboBox.SelectedItem.ToString();
                this.elment.Colour = this.colorcomboBox.SelectedItem.ToString();
                this.elment.ShowType = this.showTypecomboBox.SelectedItem.ToString();
                base.DialogResult = DialogResult.OK;
            }
        }

        private void panelDown_MouseClick(object sender, MouseEventArgs e)
        {
            this.panelDown.Focus();
        }

        private void FcmsPreviewForm_Load(object sender, EventArgs e)
        {
            base.ClientSize = new Size(realWidth * this.scrCoeff, (realHeight * this.scrCoeff) + this.panelDown.Height);
            this.Text = "120×96情报板屏幕预览";
            this.lableTextPrewiew.Location = new Point(0, 0);
            this.realXY.Text = this.realX.ToString("000") + "，" + this.realY.ToString("000");
            this.htextBox.Text = this.s_height;
            this.wtextBox.Text = this.s_width;
            if ((this.elment.MsgX == "") || (this.elment.MsgY == ""))
            {
                this.elment.MsgX = "000";
                this.elment.MsgY = "000";
            }
            this.XnumericUpDown.Value = int.Parse(this.elment.MsgX);
            this.YnumericUpDown.Value = int.Parse(this.elment.MsgY);
            this.fontcomboBox.SelectedItem = this.elment.Font;
            this.fontSizecomboBox.SelectedItem = this.elment.FontSize;
            this.colorcomboBox.SelectedItem = this.elment.Colour;
            this.showTypecomboBox.SelectedItem = this.elment.ShowType;
            
            int index = this.elment.Msg.IndexOf(@"\n");
            if (index == -1)
            {
                this.msgtextBox.Text = this.elment.Msg;
            }
            else
            {
                string msg = this.elment.Msg;
                //while (index != -1)
                //{
                //    string str2 = msg.Remove(index, 2);                    
                //    this.msgtextBox.Text = string.Concat(new object[] { str2.Substring(0, index), ch2, ch, str2.Substring(index, str2.Length - index) });                    
                //    index = this.msgtextBox.Text.IndexOf(@"\n");
                //}

                //抛弃原版旧方法，采用新方法  by 李潞洋
                msgtextBox.Text = msg.Replace(@"\n", "\r\n");
            }
        }

        private void resetradioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.textReset();
        }

        private void textLeft()
        {
            this.XnumericUpDown.Value = 0M;
            this.YnumericUpDown.Value = (this.panelUp.Height - this.lableTextPrewiew.Height) / 2;
            this.realX = 0;
            this.realY = (this.realHeight - ((this.simFontSize / this.scrCoeff) * this.msgtextBox.Lines.Length)) / 2;
            this.realXY.Text = this.realX.ToString("000") + "，" + this.realY.ToString("000");
        }

        private void textMiddle()
        {
            //this.XnumericUpDown.Value = (this.panel1.Width - this.label1.Width) / 2;
            //this.YnumericUpDown.Value = (this.panel1.Height - this.label1.Height) / 2;
            this.realX = (this.realWidth - (((this.simFontSize / this.scrCoeff) / 2) * this.MaxLineLength())) / 2;
            this.realY = (this.realHeight - ((this.simFontSize / this.scrCoeff) * this.msgtextBox.Lines.Length)) / 2;
            this.realXY.Text = this.realX.ToString("000") + "，" + this.realY.ToString("000");
            this.lableTextPrewiew.Location = new Point((int)((this.panelUp.Width - this.lableTextPrewiew.Width) / 2), (int)((this.panelUp.Height - this.lableTextPrewiew.Height) / 2));
        }

        private void textReset()
        {
            this.XnumericUpDown.Value = 0M;
            this.YnumericUpDown.Value = 0M;
            this.realX = 0;
            this.realY = 0;
            this.realXY.Text = this.realX.ToString("000") + "，" + this.realY.ToString("000");
        }

        private void XnumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.lableTextPrewiew.Location = new Point((int)this.XnumericUpDown.Value, (int)this.YnumericUpDown.Value);
        }

        private void YnumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.lableTextPrewiew.Location = new Point((int)this.XnumericUpDown.Value, (int)this.YnumericUpDown.Value);
        }

        private void fontcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.fontcomboBox.SelectedIndex)
            {
                case 0:
                    this.simFont = "黑体";
                    break;

                case 1:
                    this.simFont = "楷体";
                    break;

                case 2:
                    this.simFont = "宋体";
                    break;

                case 3:
                    this.simFont = "仿宋";
                    break;
            }
            this.lableTextPrewiew.Font = new Font(this.simFont, (float)this.simFontSize, GraphicsUnit.Pixel);
        }
    }
}
